package com.example.springbatch_step_condition2;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.*;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/***
 *
 */
@SpringBootApplication
@EnableBatchProcessing //启用springbatch框架 需要的注解
public class SpringBatchApplication9 {

    // job调度器
    @Autowired
    private JobLauncher jobLauncher;

    // job构建工厂
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // step步骤构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Autowired
    private MyStatusDecider myStatusDecider;


    @Bean
    public Tasklet firstTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------ first Tasklet ------");

//            int i = 1 / 0 ; //模拟异常 执行异常错误

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }


    @Bean
    public Tasklet taskletA() {
        return (stepContribution, chunkContext) -> {

            System.out.println("------ A Tasklet ------");

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletB() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------ B Tasklet ------");

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletDefault() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------ default Tasklet ------");

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }


    @Bean
    public Step firstStep() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("firstStep")
                .tasklet(firstTasklet())
                .build();
    }


    @Bean
    public Step stepA() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("stepA")
                .tasklet(taskletA())
                .build();
    }

    @Bean
    public Step stepB() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("stepB")
                .tasklet(taskletB())
                .build();
    }

    @Bean
    public Step stepDefault() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("stepDefault")
                .tasklet(taskletDefault())
                .build();
    }


    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("step-chuck-condition")
                .start(firstStep())
                .next(myStatusDecider) // 自定义决策器
                .from(myStatusDecider).on("A").to(stepA())
                .from(myStatusDecider).on("B").to(stepB())
                .from(myStatusDecider).on("C").to(stepDefault())
                .end()
                .incrementer(new RunIdIncrementer())    // 使用增量器(run.id自增)
                .build();
    }


    @Bean
    public Flow flow() {
        return new FlowBuilder<Flow>("flowA")
                .start(stepA())
                .next(stepB())
                .next(stepDefault())
                .build();

    }

    public Step flowsStep(){
       return stepBuilderFactory.get("flow-steps").flow(flow()).build();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication9.class, args);
    }

}
